package com.example.hao.qq;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.hao.qq.Fragment.ContactFragment;
import com.example.hao.qq.Fragment.ConversationFragment;
import com.example.hao.qq.Fragment.NowFragment;
import com.example.hao.qq.Fragment.PluginFragment;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private LinearLayout ll_conversation;
    private LinearLayout ll_contact;
    private LinearLayout ll_now;
    private LinearLayout ll_plugin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        showFragment(0);
    }


    private void initView() {
       initTabs();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_conversation:
                showFragment(0);
                break;
            case R.id.ll_contact:
                showFragment(1);
                break;
            case R.id.ll_now:
                showFragment(2);
                break;
            case R.id.ll_plugin:
                showFragment(3);
                break;
        }

    }

    private class TabItemView {
        LinearLayout ll;
        FrameLayout fl;
        ImageView iv;

        public void selected(boolean selected) {
            ll.setSelected(selected);
        }
    }

    private TabItemView[] tabItemViews = {new TabItemView(), new TabItemView(), new TabItemView(), new TabItemView()};

    private void initTabs() {
        tabItemViews[0].iv = findViewById(R.id.iv_conversation_face);
        tabItemViews[0].ll = findViewById(R.id.ll_conversation);
        tabItemViews[0].fl = findViewById(R.id.fl_conversation);

        tabItemViews[1].iv = findViewById(R.id.iv_contact_eyes);
        tabItemViews[1].ll = findViewById(R.id.ll_contact);
        tabItemViews[1].fl = findViewById(R.id.fl_contact);

        tabItemViews[2].iv = findViewById(R.id.iv_now);
        tabItemViews[2].ll = findViewById(R.id.ll_now);
        tabItemViews[2].fl = findViewById(R.id.fl_now);

        tabItemViews[3].iv = findViewById(R.id.iv_plugin_start);
        tabItemViews[3].ll = findViewById(R.id.ll_plugin);
        tabItemViews[3].fl = findViewById(R.id.fl_plugin);

        tabItemViews[0].ll.setOnClickListener(this);
        tabItemViews[1].ll.setOnClickListener(this);
        tabItemViews[2].ll.setOnClickListener(this);
        tabItemViews[3].ll.setOnClickListener(this);
    }

    private void showTab(int nextIndex) {
        tabItemViews[nextIndex].iv.clearAnimation();
        tabItemViews[nextIndex].selected(true);

        tabItemViews[nextIndex].fl.startAnimation(AnimationUtils.loadAnimation(this, R.anim.icon_scale));

        if (currentIndex != -1) {
            tabItemViews[currentIndex].selected(false);
            Animation animation;
            if (nextIndex == 0) {
                animation = AnimationUtils.loadAnimation(this, R.anim.icon_move_left);
            } else {
                animation = AnimationUtils.loadAnimation(this, R.anim.icon_move_right);
            }
            tabItemViews[currentIndex].iv.startAnimation(animation);
        }
    }

    public static final int TAB_NUM = 4;

    private Fragment[] fragments = new Fragment[TAB_NUM];
    private int currentIndex = -1;

    public void showFragment(int nextIndex) {

        if (currentIndex == nextIndex) {
            return;
        }
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        if (fragments[nextIndex] == null) {
            ft.add(R.id.fl_container, fragments[nextIndex] = createFragment(nextIndex));
        } else {
            ft.show(fragments[nextIndex]);
        }
        if (currentIndex != -1) {
            ft.hide(fragments[currentIndex]);
        }
        ft.commit();
        showTab(nextIndex);
        currentIndex = nextIndex;
    }

    private Fragment createFragment(int nextIndex) {

        if (nextIndex < 0 || nextIndex > 3)
            throw new IndexOutOfBoundsException("你是不是傻,nextIndex只能是0-3");
        switch (nextIndex) {
            case 0:
                return fragments[nextIndex] = new ConversationFragment();

            case 1:
                return fragments[nextIndex] = new ContactFragment();

            case 2:
                return fragments[nextIndex] = new NowFragment();

            case 3:
                return fragments[nextIndex] = new PluginFragment();
        }
        return null;
    }

}
